﻿using System;
using System.Collections.Generic;
using Contracts.Entities;
using Contracts.Library;
using Model.Movie;

namespace Client.Processes
{
    public class ReadMovies
    {
        private Asynchronizer<MovieElement> asyncElement;
        private Asynchronizer async;

        public ReadMovies()
        {
            asyncElement = new Asynchronizer<MovieElement>(true);
            async = new Asynchronizer();

            var reader = new ReadMovieItemsLookup();
            var sync = new Synchronizer<IEnumerable<MovieItem>>();

            asyncElement.OnProcessing += reader.Process;
            async.OnProcessing += reader.Process;

            reader.OnCompleted += sync.Process;
            sync.OnCompleted += m => OnCompleted(m);
        }

        /// <summary>
        /// Asynchrones lesen der Movies für MovieElement
        /// </summary>
        public void Process(MovieElement element)
        {
            asyncElement.Process(element);
        }

        /// <summary>
        /// Asynchrones lesen der Movies
        /// </summary>
        public void Process()
        {
            async.Process();
        }

        public event Action<IEnumerable<MovieItem>> OnCompleted = delegate { };
    }
}
